const app = getApp()
const request = require('../../utils/https.js')
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHideToast: false,
    navList: ['粉丝数量', '登录时间', '购物时间'],
    active: 0,
    inputValue: '', //搜索的内容
    pageNum: 1,
    Index: 0,
    search: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '查询中～',
    })
    const that = this;
    userId = wx.getStorageSync('userId');
    //userId = 'cbe1a96d921846c9b40a2514cbcaa228'
    //我的所有粉丝列表
    request._get(getApp().Api_url + 'fans/entry', {
      userId: userId,
      kinds: 1,
      pageNum: that.data.pageNum,
      pageSize: 10,
      sorts: that.data.Index
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('所有粉丝列表==>', res.data.data)
        wx.hideLoading();
        let orderList = res.data.data;
        that.setData({
          orderList: orderList

        })
      }
    }, function () {})
  },
  goShopping() {
    wx.switchTab({
      url: '/pages/list/list',
    })
  },
  //自定义弹框
  onToast() {
    const that = this;
    that.setData({
      isHideToast: true
    })
  },
  shutToast() {
    const that = this;
    that.setData({
      isHideToast: false
    })
  },
  //她的粉丝
  goQbfs(e) {
    let userId = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/zsfs/zsfs?title=TA的粉丝' + '&userId=' + userId
    })
  },
  onChange(e) {
    const that = this;
    console.log('e.detail===>', e.detail)
    if (!e.detail) {
      wx.showLoading({
        title: '查询中～',
      })
      that.setData({ //初始化page
        pageNum: 1,
        search: false
      });
      request._get(getApp().Api_url + 'fans/entry', {
        userId: userId,
        sorts: that.data.Index,
        pageNum: 1,
        pageSize: 10
      }, function (res) {
        if (res.data.errMsg == 'OK') {
          console.log('管家列表==>', res.data.data)
          wx.hideLoading();
          that.setData({
            orderList: res.data.data
          })
        }
      }, function () {})
    } else {
      console.log('不空')
      that.setData({
        search: true
      })
    }
  },
  //搜索功能
  onSearch(e) {
    wx.showLoading({
      title: '查询中～',
    })
    const that = this;
    that.setData({
      nickName: e.detail
    })
    request._get(getApp().Api_url + 'fans/entrySearch', {
      nickName: e.detail,
      userId: userId,
      sorts: that.data.Index,
      pageNum: 1,
      pageSize: 10
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        wx.hideLoading();
        that.setData({
          orderList: res.data.data
        })
      }
    }, function () {})
  },
  onNav(e) {
    const that = this;
    let Index = e.currentTarget.dataset.index;
    wx.showLoading({
      title: '查询中～',
    })
    that.setData({
      Index: Index
    })

    if (that.data.search) {
      console.log('搜索')
      var url = 'fans/entrySearch'
      var data = {
        nickName: that.data.nickName,
        userId: userId,
        sorts: that.data.Index,
        pageNum: 1,
        pageSize: 10
      }
    } else {
      console.log('不搜索')
      var url = 'fans/entry'
      var data = {
        userId: userId,
        sorts: that.data.Index,
        pageNum: 1,
        pageSize: 10
      }
    }

    request._get(getApp().Api_url + url, data, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        wx.hideLoading();
        that.setData({
          orderList: res.data.data
        })
      }
    }, function () {})

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;

    // console.log('搜索框是否有东西===>',)
    that.setData({
      pageNum: that.data.pageNum + 1
    })
    if (that.data.search) {
      var url = 'fans/entrySearch'
      var data = {
        nickName: that.data.nickName,
        userId: userId,
        sorts: that.data.Index,
        pageNum: that.data.pageNum,
        pageSize: 10
      }
    } else {
      var url = 'fans/entry'
      var data = {
        userId: userId,
        pageNum: that.data.pageNum,
        sorts: that.data.Index,
        pageSize: 10
      }
    }
    request._get(getApp().Api_url + url, data, function (res) {
      if (res.data.errMsg == 'OK') {
        console.log('管家列表==>', res.data.data)
        console.log('that.data.DataIntergral==>', that.data.orderList)
        if (!res.data.data || res.data.data.length <= 0 || that.data.orderList == null) {
          wx.showToast({
            icon: 'none',
            title: '已经到底了~~',
          })
        } else {
          let orderList = that.data.orderList;
          let newOrderList = res.data.data;
          that.setData({
            orderList: orderList.concat(newOrderList)
          })
        }

      }
    }, function () {})
  }
})